package com.nursinghome.aop;

import com.nursinghome.annotation.Log;
import com.nursinghome.bean.SysLog;
import com.nursinghome.common.service.SysLogService;
import com.nursinghome.utils.JwtUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Map;

@Component
@Aspect
public class LogAop {
    @Autowired
    private SysLogService sysLogService;

    //切入点 选择对那方法将进行增强
    @Pointcut("@annotation(com.nursinghome.annotation.Log)")
    public void pt01(){

    }


    @Around("pt01()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        SysLog sysLog = new SysLog();
        //获取被增强类和方法的信息
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        //获取被增强的方法对象
        Method method = methodSignature.getMethod();
        //从方法中解析注解
        String name = null;
        if(method != null){
            Log logAnnotation = method.getAnnotation(Log.class);
            name = logAnnotation.name();
        }
        //获取需要纪律日志信息
        RequestAttributes reqa = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)reqa;
        HttpServletRequest request = sra.getRequest();
        //将日志信息保存在数据库中
        //获取用户名
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        String username = (String)map.get("ename");


        //登录IP
        String ipAddr = getIpAddr(request);
        //操作时
        LocalDateTime methodTime = LocalDateTime.now();
        //保存到数据库（操作日志） log
        sysLog.setUserName(username);
        sysLog.setMethodType(name);
        sysLog.setMethodTime(methodTime);
        sysLog.setIpAddr(ipAddr);

        sysLogService.insert(sysLog);
        //运行被增强的方法
        return joinPoint.proceed();
    }
    public String getIpAddr(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getRemoteAddr();
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }

}
